﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace Miao.Stand
{
    public class HediffCompProperties_SeverityPerDayBecomeStandMaster : HediffCompProperties_SeverityPerDay
    {

        public HediffCompProperties_SeverityPerDayBecomeStandMaster()
        {
            compClass = typeof(HediffComp_SeverityPerDayBecomeStandMaster);
        }
    }
    
    public class HediffComp_SeverityPerDayBecomeStandMaster : HediffComp_SeverityPerDay
    {
        List<Hediff_Injury> resultHediffs = new List<Hediff_Injury>();

        //private HediffCompProperties_SeverityPerDayBecomeStandMaster Props => (HediffCompProperties_SeverityPerDayBecomeStandMaster)props;

        //bool hasStand = false;
        int lastStage = -1;
        Color diseaseColor = new Color(0.9f, 1.0f, 0.35f);
        public override void CompPostTick(ref float severityAdjustment)
        {
            //base.CompPostTick(ref severityAdjustment);
            if (base.Pawn.IsHashIntervalTick(200))
            {
                float num = SeverityChangePerDay();
                num *= 0.00333333341f;
                severityAdjustment += num;//严重性调整
                 if (lastStage != parent.CurStageIndex)
                {
                    lastStage = parent.CurStageIndex;
                    if (parent.CurStageIndex != 0)
                    {
                        float rd = Rand.Range(0.0f, 1.0f);
                        foreach (var f in Pawn.health.hediffSet.hediffs)//坏疾病颜色降低成为概率
                        {
                            if (f.def.defaultLabelColor == diseaseColor)
                            {
                                rd -= 0.1f;
                            }
                        }
                        if (rd > Stand.Settings.AwakeStandProbability||Pawn.WorkTagIsDisabled(WorkTags.Violent))//不能暴力的人不能成为替身使者
                        {
                            //List<BodyPartRecord> allPartsList = Pawn.def.race.body.AllParts;

                            var bodyPart = Pawn.health.hediffSet.GetRandomNotMissingPart(DamageDefOf.Rotting, BodyPartHeight.Middle, BodyPartDepth.Inside);
                            DamageInfo damageInfo = new DamageInfo(DamageDefOf.Rotting, 999, 999, -1, null, bodyPart);
                            this.Pawn.Kill(damageInfo);
                            if (Pawn?.Faction == Faction.OfPlayer)
                            {
                                Messages.Message($"{Pawn.LabelShort}成为不了替身使者，痛苦的死去了".Translate(), MessageTypeDefOf.PawnDeath);
                            }
                        }
                    }
                    else
                    {
                        Pawn.health.hediffSet.GetHediffs(ref resultHediffs, null);

                        foreach (var damage in resultHediffs)
                        {
                            //Pawn.RaceProps.body.GetPartsWithDef(.);
                            damage.Severity = damage.Severity / 4;
                            
                            HediffComp_TendDuration hediffComp_TendDuration = damage.TryGetComp<HediffComp_TendDuration>();
                            hediffComp_TendDuration.CompTended(2, 2);

                        }

                        Trait trait = new Trait(StandDefOf.StandMaster);
                        Pawn.story.traits.GainTrait(trait);
                        ChoiceLetter letter =  LetterMaker.MakeLetter("一位新的替身使者诞生了", $"{Pawn.LabelShort}成为了一名新的替身使者，并驱使着{Pawn.GetStandHediff().Label}的力量，刚成为替身使者的他非常强大".Translate(), LetterDefOf.PositiveEvent, Pawn, Faction.OfPlayer);//没有效果
                        Find.LetterStack.ReceiveLetter(letter);
                    }
                }

            }
        }
    }
}
